<template>
  <div class="educationadd">
    <div class="home">
      <div class="navheaders">
        <van-nav-bar @click-left="back">
          <template #left>
            <van-icon name="arrow-left" color="#fff" size="0.6rem" />
          </template>
          <template #title>
            <span>
              <img src="../../assets/img/logo.png" />
            </span>
          </template>
          <!-- <template #right>
            <van-icon name="wap-nav" color="#fff" size="0.6rem" />
          </template>-->
        </van-nav-bar>
      </div>
    </div>
    <div class="myumeremove">
      <div class="educationaddchild" v-show="schoolshow">
        <div class="educationaddchildcont">
          <p class="educationaddlift">教育经历</p>
        </div>
        <van-divider color="#E8E8E8" />
        <van-form @submit="onSubmitentd" label-align="left">
          <van-field readonly v-model="formdata.school" name="xxmc" @click="schoolname('school','学校名称')" label="学校名称"
            placeholder="请输入学校名称" :rules="[{ required: true, message: ' ' },
              { pattern: /^[^\s]*$/, message: ' '}]" />
          <!-- <van-divider color="#E8E8E8" /> -->
          <van-field readonly v-model="formdata.educationname" name="xl" @click="schoolname('education','学历')"
            label="学历" placeholder="请输入学历" :rules="[{ required: true, message: ' ' }]" />
          <!-- <van-divider color="#E8E8E8" /> -->
          <van-field readonly v-model="formdata.major" name="major" @click="schoolname('major','所学专业')" label="所学专业"
            placeholder="请输入所学专业" :rules="[{ required: true, message: ' ' },
              { pattern: /^[^\s]*$/, message: ' '}]" />
          <!-- <van-divider color="#E8E8E8" /> -->
          <van-field readonly clickable :value="schoolstarttime" label="开始时间" placeholder="请选择开始时间"
            @click="showTime = true" :rules="[{ required: true, message: ' ' }]" />
          <van-popup v-model="showTime" position="bottom">
            <van-datetime-picker v-model="currentDate" :maxDate="maxDatestaTm" :item-height="45" :minDate="minDate"
              type="date" @confirm="onConfirmtimestart" @cancel="showTime = false" />
          </van-popup>
          <van-field readonly clickable :value="schoolendtime" label="结束时间" placeholder="请选择结束时间"
            @click="showendTime = true" :rules="[{ required: true, message: ' ' }]" />
          <van-popup v-model="showendTime" position="bottom">
            <van-datetime-picker v-model="currentDate" type="date" :item-height="45" :maxDate="maxDate"
              :minDate="minDateendTm" @confirm="onConfirmtimeend" @cancel="showendTime = false" />
          </van-popup>
          <!-- <van-divider color="#E8E8E8" /> -->
          <div style="margin: 16px;">

            <van-button round block type="info" :style="{width:formdata.id!==''?'3rem':'8rem'}"
              :disabled="buttondisabled" native-type="submit">保存
            </van-button>
          </div>
        </van-form>
        <van-button round block type="info" style="width: 3rem;float: left;margin-left: 0.3rem;" v-if="formdata.id!==''"
          @click="removeeducat">删除该经历</van-button>
        <van-divider color="#E8E8E8" />
      </div>
    </div>

    <div>
      <experience-add ref="ExperienceAdd" v-on:industryca="industryca" v-show="!schoolshow"></experience-add>
    </div>
  </div>
</template>
<script>
import ExperienceAdd from '@/components/experienceAdd/experienceAdd'
import RESUMEMANAGEMENT from '@/api/ResumeManagement'
export default {
  components: {
    ExperienceAdd
  },
  data () {
    return {
      formdata: {
        school: '',
        education: '',
        major: '',
        educationname: '',
        id: ''
      },
      buttondisabled: false,
      maxDate: new Date(),
      minDate: new Date(1970, 0, 1),
      maxDatestaTm: new Date(),
      minDateendTm: new Date(1970, 0, 1),
      bridtime: '',
      currentDate: new Date(),
      resumeid: '',
      schoolshow: true,
      showTime: false,
      showendTime: false,
      message: '',
      schoolstarttime: '',
      schoolendtime: '',
      stateTm: new Date(2020, 0, 1),
      endTm: new Date(2020, 10, 1)
    }
  },
  computed: {},
  methods: {
    // 向子组件传值
    schoolname (namecode, name) {
      this.schoolshow = false
      this.$refs.ExperienceAdd.substate(namecode, name)
    },
    educationTime () {
      this.$router.push({
        name: 'educationTime'
      })
    },
    // 接收子组件传过来的值
    industryca (namecode, name, id) {
      console.log(namecode, name)
      this.schoolshow = true
      if (namecode === 'school') {
        this.formdata.school = name
      } else if (namecode === 'education') {
        this.formdata.educationname = name
        this.formdata.education = id
      } else if (namecode === 'major') {
        this.formdata.major = name
      }
    },
    // 开始时间
    onConfirmtimestart (date) {
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }
      this.stateTm = date[0]
      console.log(date, 'dd')
      this.schoolstarttime = `${year}-${month}-${day}`
      let timedata = new Date(this.schoolstarttime).getTime()
      timedata = new Date(timedata)
      this.minDateendTm = new Date(
        timedata.getFullYear(),
        timedata.getMonth(),
        timedata.getDate()
      )
      //  this.inputForm.cjgzdatetim = `${date.getMonth() + 1}-${date.getDate()}`
      this.showTime = false
    },
    // 结束时间
    onConfirmtimeend (date) {
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }
      this.endTm = date[0]
      this.schoolendtime = `${year}-${month}-${day}`
      let timedata = new Date(this.schoolendtime).getTime()
      timedata = new Date(timedata)
      this.maxDatestaTm = new Date(
        timedata.getFullYear(),
        timedata.getMonth(),
        timedata.getDate()
      )
      //  this.inputForm.cjgzdatetim = `${date.getMonth() + 1}-${date.getDate()}`
      this.showendTime = false
    },
    // 返回
    back () {
      if (!this.schoolshow || !this.Operatingshow) {
        this.schoolshow = true
        this.Operatingshow = true
      } else {
        this.$router.go(-1)
      }
    },
    // 教育删除
    removeeducat () {
      let that = this
      this.$dialog
        .confirm({
          title: '教育经历删除',
          message: '确定要删除该段经历么'
        })
        .then(() => {
          RESUMEMANAGEMENT.deleteEducation(that.formdata.id).then(res => {
            console.log(res.data)
            if (res.data.code === 200) {
              that.$router.push({
                name: 'resumeRemove',
                query: {
                  resumeid: that.resumeid
                }
              })
            }
          })
        })
        .catch(() => {
          // on cancel
        })
    },
    // 新增修改
    onSubmitentd () {
      this.buttondisabled = true
      let params = this.formdata
      params.startTime = this.schoolstarttime + ' 00:00:00'
      params.endTime = this.schoolendtime + ' 00:00:00'
      params.resumeId = this.resumeid
      RESUMEMANAGEMENT.insertEducation(params).then(res => {
        this.buttondisabled = false
        console.log(res.data)
        if (res.data.code === 200) {
          this.$router.push({
            name: 'resumeRemove',
            query: {
              resumeid: this.resumeid
            }
          })
        } else {
          this.$notify(res.data.msg)
        }
      })
    }
  },
  created () {
    if (this.$route.query.resumeid) {
      this.resumeid = this.$route.query.resumeid
    }
    if (this.$route.query.id) {
      let listobjet = JSON.parse(sessionStorage.getItem('educationadd'))
      console.log(listobjet)
      this.formdata.id = listobjet.id
      this.formdata.school = listobjet.school
      this.formdata.education = listobjet.education
      this.formdata.educationname = listobjet.educationText
      this.formdata.major = listobjet.major
      this.schoolstarttime = /\d{4}-\d{1,2}-\d{1,2}/g
        .exec(listobjet.startTime)
        .toString()

      this.schoolendtime = /\d{4}-\d{1,2}-\d{1,2}/g
        .exec(listobjet.endTime)
        .toString()
      this.$forceUpdate()
    }
  }
}
</script>
<style scoped></style>
